//
//  BookController.m
//  FreeTrip
//
//  Created by 352324497@qq.com on 15/11/24.
//  Copyright © 2015年 邓芮. All rights reserved.
//

#import "BookController.h"
#import "NYSegmentedControl.h"
#import "PlaceController.h"
#import "JourneyController.h"
#import "TopicsTableViewController.h"
@interface BookController ()

@property (nonatomic, strong)NYSegmentedControl *segmentedControl;

@property (nonatomic, strong)TopicsTableViewController *topicsViewVc;
@property (nonatomic, strong)JourneyController *journeyViewVc;
@property (nonatomic, strong)PlaceController *placeViewVc;
@end

@implementation BookController

-(void)initChildController
{
    self.topicsViewVc = [[TopicsTableViewController alloc]initWithStyle:UITableViewStylePlain];
    self.topicsViewVc.view.frame = CGRectMake(0, 0, KScreenWidth, KScreenHeight);
    self.topicsViewVc.view.hidden = YES;
    self.topicsViewVc.moreNum = 1;
    

    [self.view addSubview:_topicsViewVc.tableView];
    
    
    self.journeyViewVc = [[JourneyController alloc]initWithStyle:(UITableViewStylePlain)];
    self.journeyViewVc.view.frame = CGRectMake(0, 0, KScreenWidth, KScreenHeight);
    self.journeyViewVc.view.hidden = YES;
    self.journeyViewVc.moreNum = 1;
    
    [self.view addSubview:_journeyViewVc.view];
    
    self.placeViewVc = [[PlaceController alloc]initWithStyle:(UITableViewStylePlain)];
    self.placeViewVc.view.frame = CGRectMake(0, 0, KScreenWidth, KScreenHeight);
    self.placeViewVc.moreNum = 1;
    [self.view addSubview:_placeViewVc.view];
    
    [self addChildViewController:_topicsViewVc];
    [self addChildViewController:_journeyViewVc];
    [self addChildViewController:_placeViewVc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initChildController];
    
    MJRefreshAutoFooter *footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        self.placeViewVc.moreNum = self.placeViewVc.moreNum + 1;
        [self netRequestForBookPlaceWithUrl:[NSString stringWithFormat:@"http://chanyouji.com/api/destinations/attractions/%ld.json?per_page=20&page=%ld", (long)self.Id,(long)self.placeViewVc.moreNum]];
        [self.placeViewVc.tableView.mj_footer endRefreshing];
        
    }];
    
    self.placeViewVc.tableView.mj_footer = footer;

    
    
    self.segmentedControl = [[NYSegmentedControl alloc]initWithItems:@[@"旅行地", @"行程", @"专题"]];
    self.segmentedControl.selectedSegmentIndex = 0;
    // 未点击的字体的颜色
    self.segmentedControl.titleTextColor = [UIColor darkGrayColor];
    
    // 点击的字体的颜色
    self.segmentedControl.selectedTitleTextColor = [UIColor blackColor];
    self.segmentedControl.selectedTitleFont = [UIFont systemFontOfSize:16.0 * KScreenWidth / 375];
    
    // 点击时候的颜色
    self.segmentedControl.segmentIndicatorBackgroundColor = [UIColor colorWithRed:21 / 255.0 green:170 / 255.0 blue:199 / 255.0 alpha:1.0f];
    
    // 整体的背景颜色 (未点击的时候的颜色)
    self.segmentedControl.backgroundColor = [UIColor colorWithRed:23 / 255.0 green:177 / 255.0 blue:190 / 255.0 alpha:1];
    
    // 外围边框的宽度
    self.segmentedControl.borderWidth = 0.0f;;
    
    // 每个item 的边框的宽度
    self.segmentedControl.segmentIndicatorBorderWidth = 0.0f;
    
    // 每个 item 距离边界的宽度
//    self.segmentedControl.segmentIndicatorInset = 1.0f;
    
    self.segmentedControl.segmentIndicatorBorderColor = [UIColor colorWithRed:0.31f green:0.53f blue:0.72f alpha:1.0f];
    
    [self.segmentedControl sizeToFit];
    
    // 边框圆角弧度
    self.segmentedControl.cornerRadius = CGRectGetHeight(self.segmentedControl.frame) / 2.0f;
    
    [self.segmentedControl addTarget:self action:@selector(segmentedSelected:) forControlEvents:(UIControlEventValueChanged)];
    self.navigationItem.titleView = self.segmentedControl;
    
    NSString *url = [NSString stringWithFormat:@"http://chanyouji.com/api/destinations/attractions/%ld.json?per_page=20&page=1", (long)self.Id];
    
    [self netRequestForBookPlaceWithUrl:url];
}

-(void)segmentedSelected:(NYSegmentedControl *)segment
{
    if (segment.selectedSegmentIndex == 0)
    {
        self.placeViewVc.view.hidden = NO;
        self.journeyViewVc.view.hidden = YES;
        self.topicsViewVc.view.hidden = YES;
        NSString *url = [NSString stringWithFormat:@"http://chanyouji.com/api/destinations/attractions/%ld.json?per_page=20&page=%ld", (long)self.Id,(long)self.placeViewVc.moreNum];
        
        
        [self netRequestForBookPlaceWithUrl:url];
    }
    else if (segment.selectedSegmentIndex == 1)
    {
        self.placeViewVc.view.hidden = YES;
        self.journeyViewVc.view.hidden = NO;
        self.topicsViewVc.view.hidden = YES;
        NSString *url = [NSString stringWithFormat:@"http://chanyouji.com/api/destinations/plans/%ld.json?page=%ld", (long)self.Id,(long)self.journeyViewVc.moreNum];
        
        MJRefreshAutoFooter *footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
            self.journeyViewVc.moreNum = self.journeyViewVc.moreNum + 1;
            [self netRequestForBookJourneyWithUrl:[NSString stringWithFormat:@"http://chanyouji.com/api/destinations/plans/%ld.json?page=%ld", (long)self.Id,(long)self.journeyViewVc.moreNum]];
            [self.journeyViewVc.tableView.mj_footer endRefreshing];
            
        }];
        
        self.journeyViewVc.tableView.mj_footer = footer;
        
        [self netRequestForBookJourneyWithUrl:url];
    }
    else if (segment.selectedSegmentIndex == 2)
    {
        self.placeViewVc.view.hidden = YES;
        self.journeyViewVc.view.hidden = YES;
        self.topicsViewVc.view.hidden = NO;
        NSString *url = [NSString stringWithFormat:@"http://chanyouji.com/api/articles.json?destination_id=%ld&page=%ld", (long)self.Id,(long)self.topicsViewVc.moreNum];
        
        MJRefreshAutoFooter *footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
            self.topicsViewVc.moreNum = self.topicsViewVc.moreNum + 1;
            [self netRequestForBookTopicsWithUrl:[NSString stringWithFormat:@"http://chanyouji.com/api/articles.json?destination_id=%ld&page=%ld", (long)self.Id,(long)self.topicsViewVc.moreNum]];
            [self.topicsViewVc.tableView.mj_footer endRefreshing];
            
        }];
        
        self.topicsViewVc.tableView.mj_footer = footer;
        [self netRequestForBookTopicsWithUrl:url];
    }
}
#pragma mark ---bookPlace00请求---
- (void)netRequestForBookPlaceWithUrl:(NSString *)url
{
    
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    
    MBProgressHUD *mbp = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    mbp.labelText = @"Loading...";
    
    [[[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:url] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error)
      {
          if (data == nil)
          {
              NSLog(@"数据请求失败");
              return ;
          }
          
          NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
          for (NSDictionary *firDic in array)
          {
              BookPlaceModel *model = [[BookPlaceModel alloc] init];
              [model setValuesForKeysWithDictionary:firDic];
              [self.placeViewVc.modelArray addObject:model];
          }
          
          
          dispatch_async(dispatch_get_main_queue(), ^{
              [self.placeViewVc.tableView reloadData];
              [MBProgressHUD hideHUDForView:self.view animated:YES];
          });
          
      }] resume];
}
#pragma mark ---bookJourney00请求---
- (void)netRequestForBookJourneyWithUrl:(NSString *)url
{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    
    MBProgressHUD *mbp = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    mbp.labelText = @"Loading...";
    
    [[[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:url] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error)
      {
          if (data == nil)
          {
              NSLog(@"数据请求失败");
              return ;
          }
          
          NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
          for (NSDictionary *firDic in array)
          {
              BookJourneyModel *model = [[BookJourneyModel alloc] init];
              [model setValuesForKeysWithDictionary:firDic];
              [ self.journeyViewVc.modelArray addObject:model];
          }
          
         
          dispatch_async(dispatch_get_main_queue(), ^{
              [self.journeyViewVc.tableView reloadData];
              [MBProgressHUD hideHUDForView:self.view animated:YES];
          });
          
      }] resume];
    
}
#pragma mark ---bookTopics00请求---
- (void)netRequestForBookTopicsWithUrl:(NSString *)url
{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    
    MBProgressHUD *mbp = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    mbp.labelText = @"Loading...";
    
    [[[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:url] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error)
      {
          if (data == nil)
          {
              NSLog(@"数据请求失败");
              return ;
          }
          
          NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
          for (NSDictionary *firDic in array)
          {
              BookTopicsModel *model = [[BookTopicsModel alloc] init];
              [model setValuesForKeysWithDictionary:firDic];
              [self.topicsViewVc.modelArray addObject:model];
          }
          
          
          dispatch_async(dispatch_get_main_queue(), ^{
              [self.topicsViewVc.tableView reloadData];
              [MBProgressHUD hideHUDForView:self.view animated:YES];

          });

          
      }] resume];
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
